#include ".\hj_ufilemng.h"

/*--------------------------------------------------------------------------------
	Func :
--------------------------------------------------------------------------------*/
BOOL	CFileMng::GetFilesFromDir( FILEINFOMGR &FInfo , TCHAR* szFindDir , TCHAR* szFindExt , bool bDown )
{
	WIN32_FIND_DATA		FindFileData;
	HANDLE				hFileHandle;
	TCHAR				szCurrentPath[MAX_PATH] = {NULL,};

	_tcscpy( szCurrentPath , szFindDir );

	::_tcscat( szCurrentPath , "\\*.*" );
	hFileHandle = FindFirstFile( szCurrentPath , &FindFileData );

	if( hFileHandle != INVALID_HANDLE_VALUE )
	{
        while( FindNextFile( hFileHandle , &FindFileData ) )
		{
			if( FindFileData.dwFileAttributes != FILE_ATTRIBUTE_DIRECTORY )
			{
				if( szFindExt )
				{
					TCHAR szExt[_MAX_EXT]	= {NULL,};
					_tsplitpath( FindFileData.cFileName , NULL , NULL , NULL , szExt );
                    
					if( !_tcscmp( szExt , szFindExt ) )
					{
						FILEINFO	fi;
						fi.fd		= FindFileData;
						_tcscpy( fi.szFilePath , szCurrentPath );
						FInfo.vtFData.push_back(fi);
						FInfo.nNumOfFile++;
					}
				}
				else
				{
					FILEINFO	fi;
					fi.fd		= FindFileData;
					_tcscpy( fi.szFilePath , szCurrentPath );
					FInfo.vtFData.push_back(fi);
					FInfo.nNumOfFile++;
				}
			}
			else
			{
				if( bDown )
				{
					if( FindFileData.cFileName[0] != '.' )
					{
						memset( szCurrentPath , 0 , sizeof( TCHAR ) * MAX_PATH );
						_tcscpy( szCurrentPath , szFindDir );
						_stprintf( szCurrentPath , "%s%s%s" , szCurrentPath , "\\" , FindFileData.cFileName );
						GetFilesFromDir( FInfo , szCurrentPath , szFindExt , bDown );
					}
					else 
						continue;
				}
			}
		}
		FindClose( hFileHandle );
	}
	else
	{
		// Not Found Directory
		return FALSE;
	}
	return TRUE;
}

/*--------------------------------------------------------------------------------
	Func :
--------------------------------------------------------------------------------*/
BOOL	CFileMng::CleanDirectory( const TCHAR* szpPath , const TCHAR* szpDelExp , const DWORD dwOption )
{
	// Handle Option
	bool	bExceptFolder	= false;
	bool	bExceptExt		= false;

	if( (dwOption & HJFO_EXCEPTFOLDER) && (dwOption & HJFO_EXCEPTEXT) )
	{
		bExceptFolder		= true;
		bExceptExt			= true;
	}
	else if( dwOption & HJFO_EXCEPTFOLDER )
	{
        bExceptFolder		= true;
	}
	else if( dwOption & HJFO_EXCEPTEXT )
	{
        bExceptExt			= true;
	}
	WIN32_FIND_DATA		FindFileData;
	HANDLE				hFileHandle;
	TCHAR				szCurrentPath[MAX_PATH] = {NULL,};
	TCHAR				szDeleteFileName[MAX_PATH] = {NULL,};

	memset( &FindFileData ,	0 , sizeof( WIN32_FIND_DATA ) );
	memset( &hFileHandle ,	0 , sizeof( HANDLE ) );
	memset( szCurrentPath , 0 , sizeof( TCHAR ) * MAX_PATH );
	memset( szDeleteFileName , 0 , sizeof( TCHAR ) * MAX_PATH );

	_tcscpy( szCurrentPath , szpPath );
	_tcscat( szCurrentPath , szpDelExp );

	hFileHandle = FindFirstFile( szCurrentPath , &FindFileData );

	if( hFileHandle != INVALID_HANDLE_VALUE )
	{
		while( FindNextFile( hFileHandle , &FindFileData ) )
		{
			if( FILE_ATTRIBUTE_DIRECTORY == FindFileData.dwFileAttributes )
			{
				if( FindFileData.cFileName[0] != '.' )
				{
					memset( szCurrentPath , 0 , sizeof( TCHAR ) * MAX_PATH );
					_tcscpy( szCurrentPath , szpPath );
					_tcscat( szCurrentPath , FindFileData.cFileName );
					_tcscat( szCurrentPath , "\\" );
					CleanDirectory( szCurrentPath , szpDelExp , dwOption );
				}
				else
					continue;

				if( bExceptFolder )
				{
					bool bChk = false;
					for( m_itTCHAR = m_vtExecptFolderList.begin() ;
						m_itTCHAR != m_vtExecptFolderList.end() ;
						m_itTCHAR++ )
					{
						if( ExecptionFolder( FindFileData.cFileName , (*m_itTCHAR) ) )
						{
							bChk = true;
							break;
						}
					}
					if( !bChk )
					{
						::RemoveDirectory( szCurrentPath );
					}
				}
				else
				{
					::RemoveDirectory( szCurrentPath );
				}
			}
			if( bExceptExt )
			{
				char	szExt[_MAX_EXT];
				_tsplitpath( FindFileData.cFileName , NULL , NULL , NULL , szExt );

				bool bChk = false;
				for( m_itTCHAR = m_vtExecptExtList.begin() ;
					m_itTCHAR != m_vtExecptExtList.end() ;
					m_itTCHAR++ )
				{
                    if( ExecptionFileExt( szExt , (*m_itTCHAR ) ) )
					{
						bChk = true;
						break;
					}
				}
				if( !bChk )
				{
					memset( szCurrentPath , 0 , sizeof( char ) * MAX_PATH );
					_tcscpy( szCurrentPath , szpPath );
					_tcscpy( szDeleteFileName , szCurrentPath );
					_tcscat( szDeleteFileName , FindFileData.cFileName );
					DeleteFile( szDeleteFileName );
				}
			}
			else
			{
				memset( szCurrentPath , 0 , sizeof( char ) * MAX_PATH );
				_tcscpy( szCurrentPath , szpPath );
				_tcscpy( szDeleteFileName , szCurrentPath );
				_tcscpy( szDeleteFileName , FindFileData.cFileName );
				DeleteFile( szDeleteFileName );
			}
		}
		FindClose( hFileHandle );
	}
	else
	{
		// Not Fuond Directory
		return FALSE;
	}
	return TRUE;
}

/*--------------------------------------------------------------------------------
	Func :
--------------------------------------------------------------------------------*/
BOOL CFileMng::SearchFile( TCHAR* szFilePath )
{
	TCHAR* szCurFilePath	= szFilePath;
	WIN32_FIND_DATA			FindFileData;
	HANDLE					hFileHandle;

	memset( &FindFileData , 0 , sizeof( WIN32_FIND_DATA ) );
	memset( &hFileHandle , 0 ,  sizeof( HANDLE ) );

	TCHAR	szDrive[_MAX_DRIVE] = {NULL,};
	TCHAR	szFolder[_MAX_DIR]	= {NULL,};
	TCHAR	szFName[_MAX_FNAME]	= {NULL,};
	TCHAR	szExt[_MAX_EXT]		= {NULL,};

	TCHAR	szTemp[MAX_PATH]	= {NULL,};
	TCHAR	_szTemp[MAX_PATH]	= {NULL,};

	_tsplitpath( szCurFilePath , szDrive , szFolder , szFName , szExt );

	_stprintf( szTemp , "%s%s%s" , szDrive , szFolder , "*.*" );
	_stprintf( _szTemp , "%s%s" , szFName , szExt );

	hFileHandle = FindFirstFile( szTemp , &FindFileData );

	if( hFileHandle != INVALID_HANDLE_VALUE )
	{
		while( FindNextFile( hFileHandle , &FindFileData ) )
		{
			if( FILE_ATTRIBUTE_DIRECTORY == FindFileData.dwFileAttributes )
			{
				continue;
			}

			if( !_tcscmp( _szTemp , FindFileData.cFileName ) )
			{
				return TRUE;
			}
		}
        FindClose( hFileHandle );
	}
	return FALSE;
}

/*--------------------------------------------------------------------------------
	Func :
--------------------------------------------------------------------------------*/
INT	CFileMng::NumOfFileCount( TCHAR* szpPath , DWORD dwOption )
{
	TCHAR*	szCurPath	= szpPath;
	WIN32_FIND_DATA		FindFileData;
	HANDLE				hFileHandle;
	int					nFileCnt	= 0;

	memset( &FindFileData , 0 , sizeof( WIN32_FIND_DATA ) );
	memset( &hFileHandle , 0 , sizeof( HANDLE ) );
	hFileHandle = FindFirstFile( szCurPath , &FindFileData );

	if( hFileHandle != INVALID_HANDLE_VALUE )
	{
		while( FindNextFile( hFileHandle , &FindFileData ) )
		{
			if( ( HJFO_LOWFOLDER == dwOption || HJFO_LOWFNAME == dwOption ) && 
				FILE_ATTRIBUTE_DIRECTORY == FindFileData.dwFileAttributes )
			{
				if( FindFileData.cFileName[0] != '.' )
				{
					memset( szCurPath , 0 , sizeof( TCHAR ) * MAX_PATH );
					_tcscpy( szCurPath , szpPath );
					_tcscat( szCurPath , szpPath );
					_tcscat( szCurPath , "\\" );
					nFileCnt = NumOfFileCount( szCurPath , dwOption );
				}
				else
				{
					continue;
				}
			}
			if( FindFileData.dwFileAttributes != FILE_ATTRIBUTE_DIRECTORY )
			{
				if( HJFO_ONLYCURFNAME == dwOption || HJFO_LOWFNAME )
				{
					if( !_tcscmp( FindFileData.cFileName , szpPath ) )
						nFileCnt++;
				}
				else
					nFileCnt++;
			}
		}
		FindClose( hFileHandle );
	}
	else
	{
		FindClose( hFileHandle );
        // Not Found Directory
		return -1;
	}

	return nFileCnt;
}

/*--------------------------------------------------------------------------------
	Func :
--------------------------------------------------------------------------------*/
TCHAR* CFileMng::DumpFileName( TCHAR* szpPath , TCHAR* szpFileName )
{
	TCHAR	szRetName[_MAX_FNAME] = {NULL,};
	TCHAR*	szTemp	= szpPath;
	TCHAR	szCurPath[MAX_PATH] = {NULL,};

	WIN32_FIND_DATA		FindFileData;
	HANDLE				hFileHandle;
	int					nCnt	= 0;

	_tcscpy( szRetName , szpFileName );
	memset( &FindFileData , 0 , sizeof( WIN32_FIND_DATA ) );
	memset( &hFileHandle , 0 , sizeof( HANDLE ) );

	_stprintf( szCurPath , "%s%s" , szTemp , "\\*.*" );

	hFileHandle = FindFirstFile( szCurPath , &FindFileData );

	if( hFileHandle != INVALID_HANDLE_VALUE )
	{
		while( FindNextFile( hFileHandle , &FindFileData ) )
		{
			if( FindFileData.dwFileAttributes != FILE_ATTRIBUTE_DIRECTORY )
			{
				if( !_tcscmp( FindFileData.cFileName , szRetName ) )
				{
					TCHAR	strAllName[MAX_PATH]	= {NULL,};
					TCHAR	szFName[_MAX_FNAME]		= {NULL,};
					TCHAR	szExt[_MAX_EXT]			= {NULL,};
                    
					_tcscpy( strAllName , szRetName );

					_tsplitpath( strAllName , NULL , NULL , szFName , szExt );
					szExt[strlen(szExt)] = 0;

					nCnt++;
					TCHAR	szLastName[10] = {NULL,};
					_stprintf( szLastName , "(%d)" , nCnt );

					if( nCnt > 1 )
					{
						TCHAR	szTemp[_MAX_FNAME] = {NULL,};
						int		nLen	= 0;
                        itoa( nCnt , szTemp , 10 );
						nLen = (int)strlen( szTemp );
						for( int i = 0 ; i < nLen + 2 ; i++ )
                            szFName[strlen(szFName) - 1] = NULL;
					}

					memset( szRetName , 0 , sizeof( TCHAR ) * _MAX_FNAME );
					_stprintf( szRetName , "%s%s%s" , szFName , szLastName , szExt );

					FindClose( hFileHandle );
					memset( &FindFileData , 0 , sizeof( WIN32_FIND_DATA ) );
					memset( &hFileHandle , 0 , sizeof( HANDLE ) );
					hFileHandle = FindFirstFile( szCurPath , &FindFileData );
				}
			}
		}
		FindClose( hFileHandle );
	}
	else
	{
		FindClose( hFileHandle );

		return NULL;
	}

	return ( static_cast<TCHAR*>(szRetName));
}

/*--------------------------------------------------------------------------------
	Func :
--------------------------------------------------------------------------------*/
BOOL	CFileMng::AddFolder( const TCHAR* szpFolderName , const TCHAR* szpParentFolderName , const TCHAR* szpDrive )
{
	bool	bFoundParentFolder	= false;
	FOLDERINFO* pParentFolder	= NULL;
	FOLDERINFO* pNewFolder		= new FOLDERINFO;

	if( NULL == szpFolderName )
	{
		delete pNewFolder;
		pNewFolder = NULL;
		return ERR_INPUT_FOLDERNAME;
	}
	else if( 0 == m_vtCreateFolderList.size() && NULL == szpDrive )
	{
		delete pNewFolder;
		pNewFolder	= NULL;
		return ERR_INPUT_DRIVENAME;
	}
	else if( 0 == m_vtCreateFolderList.size() && szpParentFolderName )
	{
		delete pNewFolder;
		pNewFolder	= NULL;
		return ERR_NOTSECONDCASE;
	}

	if( NULL == szpParentFolderName )
	{
		ReleaseFolder();
		_tcscpy( pNewFolder->szTopName , szpFolderName );
		_tcscpy( pNewFolder->szName	, szpFolderName );
		_stprintf( pNewFolder->szPath , "%s : \\%s " , szpDrive , szpFolderName );
		m_vtCreateFolderList.push_back( pNewFolder );
		return TRUE;
	}

	for( m_itFolderInfo = m_vtCreateFolderList.begin();
		m_itFolderInfo != m_vtCreateFolderList.end();
		m_itFolderInfo++ )
	{
		FOLDERINFO* pTemp = (*m_itFolderInfo );
		if(!_tcscmp( pTemp->szName , szpFolderName ) )
		{
			pNewFolder->nIdenticalNameID = pNewFolder->nIdenticalNameID++;
		}

		if( !_tcscmp( pTemp->szName , szpParentFolderName ) && !bFoundParentFolder )
		{
			bFoundParentFolder = true;
			_tcscpy( pNewFolder->szTopName , pTemp->szTopName );
			_stprintf( pNewFolder->szPath , "%s\\%s" , pTemp->szPath , szpFolderName );
            
			if( szpDrive && strncmp( pTemp->szPath , szpDrive , 1 ) )
			{
				delete pNewFolder;
				pNewFolder	= NULL;
				return ERR_DIFFERENT_DRIVE;
			}

			if( pTemp->pChild )
			{
				FOLDERINFO* _pTemp = NULL;
				for( _pTemp = pTemp->pChild ; _pTemp->pNext ; _pTemp = _pTemp->pNext ) {}

				_pTemp->pNext		= pNewFolder;
				pNewFolder->pPrev	= _pTemp;
				pNewFolder->pParent	= pTemp;
			}
			else
			{
				pTemp->pChild		= pNewFolder;
				pNewFolder->pParent	= pTemp;
			}
			_tcscpy( pNewFolder->szTopName , pTemp->szTopName );
		}
	}

	_tcscpy( pNewFolder->szName , szpFolderName );

	if( !bFoundParentFolder )
	{
		delete pNewFolder;
		pNewFolder = NULL;
		return ERR_NOTFOUND_PARENT;
	}
	else
	{
		m_vtCreateFolderList.push_back( pNewFolder );
		return TRUE;
	}
}

/*--------------------------------------------------------------------------------
	Func :
--------------------------------------------------------------------------------*/
BOOL CFileMng::CreateFolder()
{
	if( 0 == m_vtCreateFolderList.size() )
	{
		return ERR_NOTPUTONLIST;
	}

	FOLDERINFO* pTemp = ( *m_vtCreateFolderList.begin() );

	CreateDirectory( pTemp->szPath , NULL );
	LoopCreateFolder( pTemp );

	ReleaseFolder();

	return TRUE;
}

/*--------------------------------------------------------------------------------
	Func :
--------------------------------------------------------------------------------*/
VOID	CFileMng::LoopCreateFolder( FOLDERINFO* pFolder )
{
	for( FOLDERINFO* pTemp = pFolder->pChild ; pTemp ; pTemp = pTemp->pNext )
	{
		TCHAR	szTemp[_MAX_FNAME] = {NULL,};

		if( pTemp->nIdenticalNameID > 0 )
		{
			_stprintf( szTemp , "%s(%d)" , pTemp->szPath , pTemp->nIdenticalNameID );
		}
		else
		{
			_tcscpy( szTemp , pTemp->szPath );
		}

		CreateDirectory( szTemp , NULL );

		if( pTemp->pChild )
		{
			LoopCreateFolder( pTemp );
		}
	}
}

/*--------------------------------------------------------------------------------
	Func :
--------------------------------------------------------------------------------*/
VOID	CFileMng::ReleaseFolder()
{
	for( m_itFolderInfo = m_vtCreateFolderList.begin() ;
		m_itFolderInfo != m_vtCreateFolderList.end() ;
		m_itFolderInfo++ )
	{
        delete (*m_itFolderInfo);
		(*m_itFolderInfo) = NULL;
	}

	m_vtCreateFolderList.clear();
}

/*--------------------------------------------------------------------------------
	Func :
--------------------------------------------------------------------------------*/
void	CFileMng::ReleaseFileMng()
{
	ReleaseFolder();
}






































